package com.example.zg.zg_312_ordering_new.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.beans.LoginBean;
import com.example.zg.zg_312_ordering_new.listener.RetrofitListener;
import com.example.zg.zg_312_ordering_new.model.UserModel;

import retrofit2.Call;

public class LoginActivity extends AppCompatActivity implements RetrofitListener<LoginBean> {
    private EditText username,userpass;
    private Button btn_login;
    private TextView tv_reg;
    public static String User_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    private void initViews(){
        username=(EditText)findViewById(R.id.username);
        userpass=(EditText)findViewById(R.id.et_reg_user);
        btn_login=(Button)findViewById(R.id.button_login);
        tv_reg=(TextView) findViewById(R.id.button_register);
    }
    private void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    login();
                }
            }
        });
        tv_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);

                switch (v.getId()){
                    case R.id.button_login: break;
                    case R.id.button_register:break;}
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        String et_user=data.getStringExtra("user");
        username.setText(et_user);
    }

    private void login() {
        UserModel userModel=new UserModel();
        userModel.userLogin(username.getText().toString().trim(),userpass.getText().toString().trim(),this);
    }
    @Override
    public void onSuccess(LoginBean userId, int flag){
        User_id=userId.getUserid().toString();
        if (userId.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }else {
            saveUser(userId.getUserid());
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }



    public void saveUser(String user_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        int userid=Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交修改
    }
}
